package BLL;

import DAL.connectDatabase;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *  登录与注册
 */
public class loginRegistration extends connectDatabase {

    //登录
    public Boolean longing(String name,String password)  {
        Boolean flag=false;

        Connection connect = new connectDatabase().connect();
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connect.prepareStatement("SELECT * FROM login WHERE name=? AND password=?");
            int index=1;
            preparedStatement.setString(index++,name);
            preparedStatement.setString(index,password);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                flag=true;
            }
            preparedStatement.close();
            connect.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return flag;
    }
    //注册
    public Boolean register(String name,String password)  {
        Boolean flag=false;

        Connection connect = new connectDatabase().connect();

        PreparedStatement preparedStatement1 = null;
        try {
            PreparedStatement preparedStatement = connect.prepareStatement("INSERT INTO login (name,password) VALUES (?,?)");
            int index=1;
            preparedStatement.setString(index++,name);
            preparedStatement.setString(index,password);

            flag = preparedStatement.execute();

            preparedStatement.close();
            connect.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return flag;

    }
}
